/*
 * Created: 2022-08-31 20:17:50
 * Author : 涛涛涛
 */
import React, { FC, useContext, useEffect, useState } from 'react';
import { ProductExhibitionContext } from '@/pages/ProductExhibition/ProductExhibitionContext';

export const useFinishTime = () => {
    const {
        product: { dropEndDate },
    } = useContext(ProductExhibitionContext);
    const [date, setDate] = useState('');
    const [hour, setHour] = useState({
        time: '00:00',
        xm: 'am',
    });
    useEffect(() => {
        if (!dropEndDate) return;
        const [oriDate, oriHour] = dropEndDate.split(' ');
        setDate(oriDate.replace(/-/g, '.'));
        const partHourArr = oriHour.split(':');
        if (parseInt(partHourArr[0]) > 12) {
            partHourArr[0] = String(parseInt(partHourArr[0]) - 12).padStart(2, '0');
            partHourArr[2] = 'pm';
        } else {
            partHourArr[2] = 'am';
        }
        setHour({
            time: `${partHourArr[0]}:${partHourArr[1]}`,
            xm: partHourArr[2],
        });
    }, [dropEndDate]);
    return {
        date,
        hour,
    };
};
